package com.heima.common.exception;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@SuppressWarnings("ALL")
/**
 * @Description :
 * @author     :LD
 */
@RestControllerAdvice//控制器增强注解
@Log4j2
public class GlobalExceptionCatch {
    //捕获Exception此类的异常
    @ExceptionHandler(Exception.class)
    public ResponseResult exception(Exception e){
        //控制台打印异常信息
        e.printStackTrace();
        //日志记录
        log.error("catch exception:{}",e.getMessage());
        //返回通用异常
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    @ExceptionHandler(CostomException.class)
    public ResponseResult exception(CostomException e){
        e.printStackTrace();
        log.error("catch exception:{}",e.getMessage());
        return e.getResponseResult();
    }
}
